package com.athuahua.spzx.service.impl;

import cn.hutool.core.date.DateUtil;
import com.athuahua.spzx.entity.OrderStatistics;
import com.athuahua.spzx.mapper.OrderStatisticsMapper;
import com.athuahua.spzx.dto.order.OrderStatisticsDto;
import com.athuahua.spzx.vo.order.OrderStatisticsVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.athuahua.spzx.entity.OrderInfo;
import com.athuahua.spzx.service.OrderInfoService;
import com.athuahua.spzx.mapper.OrderInfoMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
* @author huahua
* @description 针对表【order_info(订单)】的数据库操作Service实现
* @createDate 2024-08-06 14:41:46
*/
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo>
    implements OrderInfoService{

    @Resource
    private OrderInfoMapper orderInfoMapper;

    @Resource
    private OrderStatisticsMapper orderStatisticsMapper;




    @Override
    public OrderStatisticsVo getOrderStatisticsData(OrderStatisticsDto orderStatisticsDto) {
        OrderStatisticsVo result = new OrderStatisticsVo();
        List<String> dateList = new ArrayList<>();
        List<BigDecimal> amountList = new ArrayList<>();
        //日期封装到一个集合中 总金额封装到另外一个集合中
        LambdaQueryWrapper<OrderStatistics> wrapper = new LambdaQueryWrapper<>();
        wrapper
                .ge(!orderStatisticsDto.getCreateTimeBegin().isEmpty(), OrderStatistics::getOrderDate, orderStatisticsDto.getCreateTimeBegin())
                .le(!orderStatisticsDto.getCreateTimeEnd().isEmpty(), OrderStatistics::getOrderDate, orderStatisticsDto.getCreateTimeEnd())
                .orderByAsc(OrderStatistics::getOrderDate);
        List<OrderStatistics> orderStatistics = orderStatisticsMapper.selectList(wrapper);

        for(OrderStatistics item : orderStatistics){
            dateList.add(DateUtil.format(item.getOrderDate(), "yyyy-MM-dd"));
            amountList.add(item.getTotalAmount());
        }
        result.setAmountList(amountList);
        result.setDateList(dateList);


        return result;
    }
}




